app.controller('sys_regionController', ['$scope', 'httpWrapService', 'dialogs', '$state', '$stateParams','uiGridConstants',
        function($scope, httpWrapService, dialogs, $state, $stateParams,uiGridConstants) {

    var columnDefs = [
        {name: "区域名称", field: "name",maxWidth: 250, cellTemplate: '<div class="ui-grid-cell-contents" title="TOOLTIP"><span ng-repeat="i in row.entity.$$treeLevel | range">&nbsp;&nbsp;</span>{{row.entity.name}}</div>'
        },
        {name: "区域编码", field: "code",maxWidth: 120} ,
        {name: "区域类型", field: "type",maxWidth: 120,
            cellFilter: 'dictionaryFiter : "sysAreaType"',
            filter : {
                noTerm: true,
                type: uiGridConstants.filter.SELECT,
                selectOptions : $scope.dictionary.sysAreaType
            }} ,
        {name: "备注", field: "remarks",
            filter : {
                type :  uiGridConstants.filter.CONTAINS
            }} ,
        {
            displayName: "",
            field: "id",
            enableFiltering: false,
            enableSorting: false,
            maxWidth: 90,
            cellTemplate: '<button class="btn btn-default m-b-xs w-xs" ng-click="grid.appScope.view(this)">查看/编辑</button>'
        },
        {
            displayName: "",
            field: "id",
            enableFiltering: false,
            enableSorting: false,
            maxWidth: 90,
            cellTemplate: '<button class="btn btn-danger m-b-xs w-xs" ng-disabled="grid.appScope.Grid.deletes[\'{{COL_FIELD}}\']" ng-click="deleteBtnDisable[id] = true; grid.appScope.delete(this);">删除</button>'
        }
    ];

    $scope.gridOptions  = {
        columnDefs: columnDefs,
        enableColumnResizing: true,
        enablePaginationControls : false,
        enableFiltering: true,
        enableFullRowSelection: true,
        showTreeExpandNoChildren: false,
        multiSelect : false,
        data : [],
        onRegisterApi : function(gridApi){
            $scope.gridApi = gridApi;
        }
    }



    httpWrapService.post("system/region/list.do", {}).then(function(res){
        $scope.gridOptions.data = res.data.data;
        setTimeout(function(){
            $scope.gridApi.treeBase.expandAllRows();
        }, 300)
    });


    $scope.add = function() {
        var rows = $scope.gridApi.selection.getSelectedRows();
        if(rows.length > 1) {
            dialogs.error('错误', '不能多选，请选择一个区域后增加', {size: 'md'});
            return ;
        }
        var parentId;
        if(rows[0]){
            parentId = rows[0].id;
        }
        $state.go('app.system.region.view', {type : "add", parentId : parentId});
    };

    $scope.view = function(target) {
        $state.go('app.system.region.view', {id : target.$parent.row.entity.id, type:"update" });
    };

    $scope.delete = function(target) {
        var id = target.$parent.row.entity.id;
        httpWrapService.defaultDelete('system/region/delete/'+ id +'.do').then(function(res){
            var index = $scope.gridOptions.data.indexOf(target.$parent.row.entity);
            $scope.gridOptions.data.splice(index, 1);
        });
    };

}]);
app.filter('range', function(){
    return function(n) {
        var res = [];
        for (var i = 0; i < n; i++) {
            res.push(i);
        }
        return res;
    };
});